import 'package:easy_refresh/easy_refresh.dart';
import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:taonile/config/config.dart';
import 'package:taonile/entity/getcash_record_entity.dart';
import 'package:taonile/utils/string_utils.dart';
import 'package:taonile/views/default_views.dart';

import '../entity/asset_general_detail_entity.dart';
import '../http/api.dart';
import '../routers/routers.dart';
import '../views/dialog_manager.dart';

///
/// 提现记录
///
class MineGetcashRecord extends StatefulWidget {
  MineGetcashRecord({Key? key}) : super(key: key);

  @override
  State<MineGetcashRecord> createState() => _MineGetcashRecordState();
}

class _MineGetcashRecordState extends State<MineGetcashRecord> {
  var page = 1;
  List<GetcashRecordDataData> datas = [];

  @override
  void initState() {
    super.initState();
    getDatas();
  }

  getDatas() async {
    dynamic result = await Api.getCashRecord({'page': page, 'limit': '20'});
    GetcashRecordEntity entity = GetcashRecordEntity.fromJson(result);
    setState(() {
      if (page == 1) {
        datas.clear();
      }
      if (entity.data.data.isNotEmpty) {
        page++;
        datas.addAll(entity.data.data);
      }
    });
  }

  // "status": "created", //created=申请中,successed=成功,rejected=已拒绝
  Color getMoneyColor(String status) {
    if (status == 'created') {
      return Config.colorMain;
    } else if (status == 'successed') {
      return Colors.green;
    } else if (status == 'rejected') {
      return Colors.red;
    } else {
      return Config.colorMain;
    }
  }

  String getStatusStr(String status) {
    if (status == 'created') {
      return '提现中';
    } else if (status == 'successed') {
      return '提现成功';
    } else if (status == 'rejected') {
      return '提现失败';
    } else {
      return '提现中';
    }
  }

  Widget getListItem(int index) {
    return Container(
      // height: 124.w,
      padding: EdgeInsets.symmetric(vertical: 20.w, horizontal: 20.w),
      margin: EdgeInsets.only(top: 20.w),
      decoration: BoxDecoration(
        color: (Config.colorWhite),
        borderRadius: BorderRadius.circular(12.w),
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                '提现至${datas[index].accountText.bankName}(${StringUtils.getBankCardEndNum(datas[index].accountText.bankNumber)})',
                style: TextStyle(
                  fontSize: 24.w,
                  color: Config.colorText1,
                ),
              ),
              // Padding(
              //   padding: EdgeInsets.only(top: 2.w),
              // ),
              // Text(
              //   '${datas[index].accountText.bankBranch}(${datas[index].accountText.bankNumber.substring(datas[index].accountText.bankNumber.length - 4)})',
              //   style: TextStyle(
              //     fontSize: 24.w,
              //     color: Config.colorText1,
              //   ),
              // ),
              Padding(
                padding: EdgeInsets.only(top: 15.w),
              ),
              Text(
                //datas[index].accountText.realName
                StringUtils.getNameHide(datas[index].accountText.realName),
                style: TextStyle(
                  fontSize: 24.w,
                  color: Config.colorText1,
                ),
              ),
              Padding(
                padding: EdgeInsets.only(top: 15.w),
              ),
              Text(
                datas[index].createdAt,
                style: TextStyle(
                  fontSize: 24.w,
                  color: Config.colorText3,
                ),
              ),
            ],
          ),
          Column(
            crossAxisAlignment: CrossAxisAlignment.end,
            children: [
              Text(
                '金券：${datas[index].money}',
                style: TextStyle(
                  fontSize: 24.w,
                  color: Config.colorText1,
                ),
              ),
              Padding(
                padding: EdgeInsets.only(top: 60.w),
              ),
              Text(
                getStatusStr(datas[index].status),
                style: TextStyle(
                  fontSize: 24.w,
                  color: getMoneyColor(datas[index].status),
                ),
              ),
              Visibility(
                visible: datas.isNotEmpty && datas[index].reason.isNotEmpty,
                child: Padding(
                  padding: EdgeInsets.only(top: 5.w),
                ),
              ),
              Visibility(
                visible: datas.isNotEmpty && datas[index].reason.isNotEmpty,
                child: Text(
                  datas[index].reason,
                  style: TextStyle(
                    fontSize: 22.w,
                    color: getMoneyColor(datas[index].status),
                  ),
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Config.colorBg2,
        appBar: AppBar(
          title: Text('提现记录'),
          centerTitle: true,
        ),
        body: Container(
          width: double.infinity,
          height: double.infinity,
          child: Stack(
            children: [
              Container(
                height: 90.w,
                margin: EdgeInsets.only(left: 25.w, right: 25.w, bottom: 5.w, top: 25.w),
                padding: EdgeInsets.symmetric(horizontal: 25.w),
                decoration: BoxDecoration(
                  color: (Config.colorWhite),
                  borderRadius: BorderRadius.circular(12.w),
                ),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.start,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Icon(
                      Icons.notifications_on_outlined,
                      size: 32.w,
                      color: Config.colorMain,
                    ),
                    Padding(
                      padding: EdgeInsets.only(left: 10.w),
                    ),
                    Text(
                      '提现金额一般1~3个工作日到账',
                      style: TextStyle(
                        fontSize: 26.w,
                        color: Config.colorMain,
                      ),
                    ),
                  ],
                ),
              ),
              Container(
                  width: double.infinity,
                  height: double.infinity,
                  padding: EdgeInsets.symmetric(vertical: 0, horizontal: 0.w),
                  margin: EdgeInsets.only(left: 25.w, right: 25.w, bottom: 5.w, top: 115.w),
                  child: datas.isNotEmpty
                      ? EasyRefresh(
                          header: const MaterialHeader(),
                          footer: const MaterialFooter(),
                          onRefresh: () async {
                            await Future.delayed(const Duration(milliseconds: 200), () {
                              page = 1;
                              getDatas();
                            });
                          },
                          onLoad: () async {
                            getDatas();
                          },
                          child: ListView.builder(
                            // physics: const NeverScrollableScrollPhysics(),
                            itemCount: datas.length,
                            padding: EdgeInsets.symmetric(vertical: 1.w, horizontal: 1.w),
                            itemBuilder: (BuildContext context, int index) {
                              return getListItem(index);
                            },
                          ),
                        )
                      : showNullData()),
            ],
          ),
        ));
  }
}
